import src from './source';
import * as GlobalUtil from './globalUtil';

export const isShowAI = () =>{
    return src.globalStore.urlParams.showAI === "1";
}

let scriptContent = "";
export const setScriptContent = (str="") =>{
    if(!isShowAI())
        return;
    scriptContent = str;
}

//字段变更类型 
export const TYPE_MAIN_INIT = "1";        //主表字段初始值加载
export const TYPE_DETAIL_ADDROW = "2";    //添加/生成明细行的默认值
export const TYPE_FIELD_CHANGE = "3";     //手动变更字段
export const TYPE_LINKAGE_SYNC = "4";     //同步联动修改
export const TYPE_LINKAGE_ASYNC = "5";    //异步联动修改
export const TYPE_WFFORM_API = "6";       //通过接口WfForm修改(包括公式)
export const CHANGE_FIELD_VALUE = "c_value";    //改变字段值
export const CHANGE_FIELD_VARS = "c_vars";      //改变字段属性

let modifyStep = {};
let stepIndex = 0;

/**
 * 当开启智能分析showAI=1时，记录每个字段变更的详细修改日志
 * 包括加载页面初始值、明细生成行的初始值、手动修改值、联动修改的值、接口修改的值
 */
export const generateStepId = (type, otherinfo={}) =>{
    if(!isShowAI())
        return 0;
    stepIndex++;
    modifyStep[`step_${stepIndex}`] = {
        type: type,
        datas: [],
        otherinfo: otherinfo,
        time: new Date().getTime()
    };
    return stepIndex;
}

export const appendStepDetail = (stepid, fieldMark, type, result={}) =>{
    if(!isShowAI())
        return;
    if(typeof stepid === "undefined" || stepid <= 0 || !modifyStep[`step_${stepid}`])
        return;
    const curStep = modifyStep[`step_${stepid}`];
    curStep.datas.push({fieldMark, type, result});
}

//显示分析页面
export const openAIPage = () =>{
    const { commonParam } = src.globalStore;
    const custompage = commonParam.custompage;
    window.WfForm_analyzeData = {
        custompage: {
            jsp: GlobalUtil.generateCustomPageList(),
            js: custompage.jsPage || [],
            css: custompage.cssPage || []
        },
        scriptContent: scriptContent,
        modifyStep: modifyStep,
    }
    window.open("/spa/workflow/static/index.html#/main/workflow/analyze");
}